package com.yinhe.music.yhmusic.adapter;

import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.db.bean.MenuDBEntity;
import com.yinhe.music.yhmusic.model.MyTitleItem;
import com.yinhe.music.yhmusic.model.SongMenuList;

import java.util.List;

/**
 * 我的界面-歌单列表adapter
 *
 * @author hasee
 * @date 2017/8/23
 */

public class MyAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static final int TYPE_TITLE = 0;
    public static final int TYPE_ITEM = 1;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MyAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_TITLE, R.layout.expandable_item);
        addItemType(TYPE_ITEM, R.layout.dialog_collection_item);
    }

    private void setItemClick(BaseViewHolder helper, MyTitleItem titleItem) {
        int pos = helper.getAdapterPosition();
        if (titleItem.hasSubItem()) {
            if (titleItem.isExpanded()) {
                collapse(pos);
            } else {
                expand(pos);
            }
        }
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    @Override
    protected void convert(final BaseViewHolder helper, final MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case 0:
                final MyTitleItem titleItem = (MyTitleItem) item;
                helper.setText(R.id.expand_title, titleItem.title + "(" + titleItem.count + ")")
                        .setImageResource(R.id.expand_img, titleItem.isExpanded()
                                ? R.drawable.ic_arrow_down_grey
                                : R.drawable.ic_arrow_down_right);
                helper.itemView.setOnClickListener((v) ->
                    setItemClick(helper, titleItem)
                );
                helper.addOnClickListener(R.id.expand_menu);
                break;
            case 1:
                final SongMenuList menuList = (SongMenuList) item;
                ImageView image = helper.getView(R.id.image);
                if (menuList.getPrivateX() == 1) {
                    GlideHelper.setImageResource(image, menuList.getImage(), R.drawable.ic_my_fav);
                } else {
                    GlideHelper.setImageResource(image, menuList.getImage(), R.drawable.default_cover);
                }

                helper.setText(R.id.menu_name, menuList.getSongMenuName());
                MenuDBEntity menuDBEntity = DataBaseAccessor.getInstance().getMenuByIdSyn(((SongMenuList) item).getSongMenuId());
                if (menuDBEntity != null) {
                    int songSum = menuDBEntity.getSongnum();
                    helper.setText(R.id.song_sum, songSum + "首");
                } else {
                    helper.setText(R.id.song_sum, menuList.getSongNum() + "首");
                }
                break;
            default:
                break;
        }
    }

}
